﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class CustomerBLL : IBaseInfo
    {
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private static readonly ICustomerDA CustomerDA = DataAccess.CreateCustomerDA();
        private List<string> dataBaseID;
        private static readonly IEdiLogDA edilogDA = DataAccess.CreateEdiLogDA();
        private PubParasInfo pubParasInfo;

        public CustomerBLL()
        {
            this.pubParasInfo = null;
        }

        public CustomerBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CustomerBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string CustomerImportByData(string[][] ImportByData, string IsUpdate)
        {
            string customerIDTrue = string.Empty;
            string customerIDFalse = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int i = 1; ImportByData.Length > i; i++)
                {
                    CustomerInfo customer = new CustomerInfo();
                    for (int n = 0; ImportByData[i].Length > n; n++)
                    {
                        try
                        {
                            switch (ImportByData[0][n])
                            {
                                case "客户代码":
                                {
                                    if (!string.IsNullOrEmpty(ImportByData[i][n]))
                                    {
                                        customer.CustomerID = ImportByData[i][n];
                                    }
                                    continue;
                                }
                                case "客户名称":
                                {
                                    customer.Company = ImportByData[i][n];
                                    continue;
                                }
                                case "描述":
                                {
                                    customer.Description = ImportByData[i][n];
                                    continue;
                                }
                                case "增值税号":
                                {
                                    customer.Vat = ImportByData[i][n];
                                    continue;
                                }
                                case "城市":
                                {
                                    customer.City = ImportByData[i][n];
                                    continue;
                                }
                                case "省份":
                                {
                                    customer.State = ImportByData[i][n];
                                    continue;
                                }
                                case "邮编":
                                {
                                    customer.Zip = ImportByData[i][n];
                                    continue;
                                }
                                case "国家":
                                {
                                    customer.Country = ImportByData[i][n];
                                    continue;
                                }
                                case "ISO国家码":
                                {
                                    customer.CountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "地址行":
                                {
                                    customer.Address = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1":
                                {
                                    customer.Contact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话1":
                                {
                                    customer.Phone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真1":
                                {
                                    customer.Fax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人1电子邮件":
                                {
                                    customer.Email1 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2":
                                {
                                    customer.Contact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "电话2":
                                {
                                    customer.Phone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "传真2":
                                {
                                    customer.Fax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "联系人2电子邮件":
                                {
                                    customer.Email2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B城市":
                                {
                                    customer.BCity = ImportByData[i][n];
                                    continue;
                                }
                                case "B省份":
                                {
                                    customer.BState = ImportByData[i][n];
                                    continue;
                                }
                                case "B邮编":
                                {
                                    customer.BZip = ImportByData[i][n];
                                    continue;
                                }
                                case "B国家":
                                {
                                    customer.BCountry = ImportByData[i][n];
                                    continue;
                                }
                                case "BISO国家码":
                                {
                                    customer.BCountryCode = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单地址":
                                {
                                    customer.BAddress = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人1":
                                {
                                    customer.BContact1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话1":
                                {
                                    customer.BPhone1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真1":
                                {
                                    customer.BFax1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件1":
                                {
                                    customer.BEmail1 = ImportByData[i][n];
                                    continue;
                                }
                                case "B账单联系人2":
                                {
                                    customer.BContact2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电话2":
                                {
                                    customer.BPhone2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B传真2":
                                {
                                    customer.BFax2 = ImportByData[i][n];
                                    continue;
                                }
                                case "B电子邮件2":
                                {
                                    customer.BEmail2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段1":
                                {
                                    customer.Udf1 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段2":
                                {
                                    customer.Udf2 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段3":
                                {
                                    customer.Udf3 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段4":
                                {
                                    customer.Udf4 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段5":
                                {
                                    customer.Udf5 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段6":
                                {
                                    customer.Udf6 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段7":
                                {
                                    customer.Udf7 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段8":
                                {
                                    customer.Udf8 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段9":
                                {
                                    customer.Udf9 = ImportByData[i][n];
                                    continue;
                                }
                                case "自定义字段10":
                                {
                                    customer.Udf10 = ImportByData[i][n];
                                    continue;
                                }
                                case "备注":
                                {
                                    customer.Remark = ImportByData[i][n];
                                    continue;
                                }
                                case "创建人":
                                {
                                    customer.CreatedBy = ImportByData[i][n];
                                    continue;
                                }
                            }
                        }
                        catch
                        {
                            if (!string.IsNullOrEmpty(customer.CustomerID))
                            {
                                customerIDFalse = customerIDFalse + customer.CustomerID + PublicMethod.GetInstance().GetGlobalMsgResource("C003", this.pubParasInfo.UserLanguage) + "<br/>";
                            }
                        }
                    }
                    customer.IsActive = "Y";
                    customer.UpdatedBy = customer.CreatedBy;
                    if (!string.IsNullOrEmpty(customer.CustomerID))
                    {
                        try
                        {
                            if (CustomerDA.CheckCustomerIDUnique(dataBase, tran, customer.CustomerID, this.dataBaseID[0]))
                            {
                                if (!CustomerDA.CheckAuthorizationCustomerIDUserID(dataBase, tran, customer.CustomerID, this.dataBaseID[0], this.pubParasInfo))
                                {
                                    customerIDFalse = customerIDFalse + customer.CustomerID + PublicMethod.GetInstance().GetGlobalMsgResource("C008", this.pubParasInfo.UserLanguage) + "<br/>";
                                }
                                else if (IsUpdate == "Y")
                                {
                                    if (CustomerDA.UpdateCustomer(dataBase, tran, customer, this.dataBaseID) > 0)
                                    {
                                        customerIDFalse = customerIDFalse + customer.CustomerID + PublicMethod.GetInstance().GetGlobalMsgResource("C004", this.pubParasInfo.UserLanguage) + "<br/>";
                                    }
                                }
                                else
                                {
                                    customerIDFalse = customerIDFalse + customer.CustomerID + PublicMethod.GetInstance().GetGlobalMsgResource("C005", this.pubParasInfo.UserLanguage) + "<br/>";
                                }
                            }
                            else if (CustomerDA.InsertCustomer(dataBase, tran, customer, this.dataBaseID) > 0)
                            {
                                customerIDTrue = customerIDTrue + customer.CustomerID + ";";
                            }
                        }
                        catch
                        {
                            if (!string.IsNullOrEmpty(customer.CustomerID))
                            {
                                customerIDFalse = customerIDFalse + customer.CustomerID + PublicMethod.GetInstance().GetGlobalMsgResource("C006", this.pubParasInfo.UserLanguage) + "<br/>";
                            }
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return customerIDFalse;
        }

        public string Delete(List<string> CustomerIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<CustomerInfo> customerInfos = new List<CustomerInfo>();
                CustomerInfo customerInfo = null;
                foreach (string id in CustomerIDS)
                {
                    customerInfo = CustomerDA.GetCustomerByID(dataBase, tran, id, this.dataBaseID[0], this.pubParasInfo);
                    customerInfos.Add(customerInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (CustomerDA.DeleteCustomer(dataBase, tran, CustomerIDS, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < CustomerIDS.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<CustomerInfo>(dataBase, tran, customerInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CUSTOMER", this.dataBaseID);
                        temp = temp + CustomerIDS[i] + ",";
                    }
                    temp = temp.Substring(0, temp.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return temp;
        }

        public DataSet GetAllInfo()
        {
            return CustomerDA.GetAllCustomer(this.dataBaseID[0], this.pubParasInfo);
        }

        public object GetInfoByID(string CustomerID)
        {
            return CustomerDA.GetCustomerByID(CustomerID, this.dataBaseID[0], this.pubParasInfo);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            CustomerQueryEntity CustomerQuery = obj as CustomerQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(CustomerQuery.CustomerID))
            {
                sqlWhere.Add(" C.CUSTOMER_ID " + CustomerQuery.CustomerID);
            }
            if (!string.IsNullOrEmpty(CustomerQuery.Description))
            {
                sqlWhere.Add(" C.DESCRIPTION " + CustomerQuery.Description);
            }
            if (!string.IsNullOrEmpty(CustomerQuery.Company))
            {
                sqlWhere.Add(" C.COMPANY " + CustomerQuery.Company);
            }
            if (!string.IsNullOrEmpty(CustomerQuery.City))
            {
                sqlWhere.Add(" C.CITY " + CustomerQuery.City);
            }
            if (!string.IsNullOrEmpty(CustomerQuery.IsActive))
            {
                sqlWhere.Add(" C.IS_ACTIVE " + CustomerQuery.IsActive);
            }
            return CustomerDA.GetCustomerByQueryList(sqlWhere, CustomerQuery, this.dataBaseID[0], this.pubParasInfo);
        }

        public string Insert(object obj)
        {
            string customerID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                CustomerInfo CustomerInfo = obj as CustomerInfo;
                if (CustomerDA.CheckCustomerIDUnique(dataBase, tran, CustomerInfo.CustomerID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2120_001", this.pubParasInfo.UserLanguage, new List<string> { CustomerInfo.CustomerID });
                }
                CustomerInfo.UpdatedBy = CustomerInfo.CreatedBy;
                CustomerInfo.CreatedDate = DateTime.Now;
                CustomerInfo.UpdatedDate = DateTime.Now;
                if (CustomerDA.InsertCustomer(dataBase, tran, CustomerInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CustomerInfo>(dataBase, tran, null, CustomerInfo, AuditAction.Insert, this.pubParasInfo.UserID, "CUSTOMER", this.dataBaseID);
                    customerID = CustomerInfo.CustomerID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return customerID;
        }

        public string InsertCustomerByWebService(List<CustomerInfo> customerInfo)
        {
            EdiLogInfo edilogInfo;
            string customerID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            CustomerInfo customerEx = null;
            List<EdiLogInfo> edilogInfoList = new List<EdiLogInfo>();
            try
            {
                foreach (CustomerInfo info in customerInfo)
                {
                    customerEx = info;
                    if (CustomerDA.CheckCustomerIDUnique(dataBase, tran, info.CustomerID, this.dataBaseID[0]))
                    {
                        PublicMethod.GetInstance().ShowAlertMessage("F2120_001", this.pubParasInfo.UserLanguage, new List<string> { info.CustomerID });
                    }
                    CustomerDA.InsertCustomer(dataBase, tran, info, this.dataBaseID);
                    edilogInfo = new EdiLogInfo {
                        WhID = "",
                        LogDate = DateTime.Now,
                        IsSuccess = "成功",
                        LogDescr = "客户" + info.CustomerID + "插入成功",
                        UserID = "CEAWebService",
                        Udf1 = "新增",
                        Udf2 = "客户代码:" + info.CustomerID,
                        Udf3 = "客户资料",
                        Udf4 = "客户类型:" + info.Company
                    };
                    edilogInfoList.Add(edilogInfo);
                }
                tran.Commit();
                customerID = "OK";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                edilogInfoList = new List<EdiLogInfo>();
                edilogInfo = new EdiLogInfo {
                    WhID = "",
                    LogDate = DateTime.Now,
                    IsSuccess = "异常",
                    LogDescr = "客户" + customerEx.CustomerID + "插入失败:" + ex.Message,
                    UserID = "CEAWebService",
                    Udf1 = "新增",
                    Udf2 = "客户代码:" + customerEx.CustomerID,
                    Udf3 = "客户资料",
                    Udf4 = "客户类型:" + customerEx.Company
                };
                customerID = ex.Message;
            }
            finally
            {
                edilogDA.InsertEdiLogs(edilogInfoList);
                conn.Close();
            }
            return customerID;
        }

        public string Update(object obj)
        {
            string customerID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                CustomerInfo CustomerInfo = obj as CustomerInfo;
                CustomerInfo.UpdatedDate = DateTime.Now;
                CustomerInfo oldCustomerInfo = CustomerDA.GetCustomerByID(dataBase, tran, CustomerInfo.CustomerID, this.dataBaseID[0], this.pubParasInfo);
                if (CustomerDA.UpdateCustomer(dataBase, tran, CustomerInfo, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CustomerInfo>(dataBase, tran, oldCustomerInfo, CustomerInfo, AuditAction.Update, this.pubParasInfo.UserID, "CUSTOMER", this.dataBaseID);
                    customerID = CustomerInfo.CustomerID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return customerID;
        }

        public string UpdateCustomerByWebService(List<CustomerInfo> customerInfo)
        {
            EdiLogInfo edilogInfo;
            string customerID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            CustomerInfo customerEx = null;
            List<EdiLogInfo> edilogInfoList = new List<EdiLogInfo>();
            string strCustomerID = string.Empty;
            string strCompany = string.Empty;
            try
            {
                foreach (CustomerInfo info in customerInfo)
                {
                    strCustomerID = info.CustomerID;
                    customerEx = CustomerDA.GetCustomerWebServiceByID(dataBase, tran, info.CustomerID, this.dataBaseID[0], this.pubParasInfo);
                    if (customerEx == null)
                    {
                        throw new Exception("未找到匹配数据源");
                    }
                    if (!string.IsNullOrEmpty(info.Company))
                    {
                        customerEx.Company = info.Company;
                        strCompany = info.Company;
                    }
                    if (!string.IsNullOrEmpty(info.IsActive))
                    {
                        customerEx.IsActive = info.IsActive;
                    }
                    if (!string.IsNullOrEmpty(info.Address))
                    {
                        customerEx.Address = info.Address;
                    }
                    if (!string.IsNullOrEmpty(info.Udf1))
                    {
                        customerEx.Udf1 = info.Udf1;
                    }
                    if (!string.IsNullOrEmpty(info.Udf2))
                    {
                        customerEx.Udf2 = info.Udf2;
                    }
                    if (!string.IsNullOrEmpty(info.Udf3))
                    {
                        customerEx.Udf3 = info.Udf3;
                    }
                    if (!string.IsNullOrEmpty(info.Udf4))
                    {
                        customerEx.Udf4 = info.Udf4;
                    }
                    if (!string.IsNullOrEmpty(info.Udf5))
                    {
                        customerEx.Udf5 = info.Udf5;
                    }
                    CustomerDA.UpdateCustomer(dataBase, tran, customerEx, this.dataBaseID);
                    edilogInfo = new EdiLogInfo {
                        WhID = "",
                        LogDate = DateTime.Now,
                        IsSuccess = "成功",
                        LogDescr = "客户" + customerEx.CustomerID + "更新成功",
                        UserID = "CEAWebService",
                        Udf1 = "更新",
                        Udf2 = "客户代码:" + customerEx.CustomerID,
                        Udf3 = "客户资料",
                        Udf4 = "客户类型:" + customerEx.Company
                    };
                    edilogInfoList.Add(edilogInfo);
                }
                tran.Commit();
                customerID = "OK";
            }
            catch (Exception ex)
            {
                tran.Rollback();
                edilogInfoList = new List<EdiLogInfo>();
                edilogInfo = new EdiLogInfo {
                    WhID = "",
                    LogDate = DateTime.Now,
                    IsSuccess = "异常",
                    UserID = "CEAWebService",
                    Udf1 = "更新"
                };
                if (customerEx != null)
                {
                    edilogInfo.LogDescr = "客户" + customerEx.CustomerID + "更新失败:" + ex.Message;
                    edilogInfo.Udf2 = "客户代码:" + customerEx.CustomerID;
                    edilogInfo.Udf4 = "客户类型:" + customerEx.Company;
                    edilogInfo.Udf3 = "客户资料";
                }
                else
                {
                    edilogInfo.LogDescr = "客户" + strCustomerID + "更新失败:" + ex.Message;
                    edilogInfo.Udf2 = "客户代码:" + strCustomerID;
                    edilogInfo.Udf4 = "客户类型:" + strCompany;
                    edilogInfo.Udf3 = "客户资料";
                }
                customerID = ex.Message;
            }
            finally
            {
                edilogDA.InsertEdiLogs(edilogInfoList);
                conn.Close();
            }
            return customerID;
        }
    }
}

